package com.atguigu.dga230315.governance.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga230315.governance.bean.GovernanceAssessDetail;
import com.atguigu.dga230315.governance.bean.GovernanceAssessGlobal;
import com.atguigu.dga230315.governance.bean.GovernanceAssessTecOwner;
import com.atguigu.dga230315.governance.service.GovernanceAssessDetailService;
import com.atguigu.dga230315.governance.service.GovernanceAssessGlobalService;
import com.atguigu.dga230315.governance.service.GovernanceAssessTecOwnerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 治理考评结果明细 前端控制器
 * </p>
 *
 * @author zhangjunyi
 * @since 2023-07-18
 */
@RestController
@RequestMapping("/governance")
public class GovernanceAssessDetailController {

    @Autowired
    GovernanceAssessDetailService governanceAssessDetailService;

    @Autowired
    GovernanceAssessGlobalService governanceAssessGlobalService;

    @Autowired
    GovernanceAssessTecOwnerService governanceAssessTecOwnerService;


    //1.重新评估
    @PostMapping("assess/{assessDate}")
    public String assess(@PathVariable("assessDate") String assessDate){
        governanceAssessDetailService.mainAssess(assessDate);
        return "success";
    }

    //2.全局分数
    @GetMapping("globalScore")
    public String globalScore(){
        //1.查询全局分数表
        GovernanceAssessGlobal governanceAssessGlobal = governanceAssessGlobalService.getOne(new QueryWrapper<GovernanceAssessGlobal>()
                .orderByDesc("create_time")
                .last("limit 1")
        );

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("assessDate", governanceAssessGlobal.getAssessDate());
        jsonObject.put("sumScore", governanceAssessGlobal.getScore());

        //创建List集合放五个维度的分数
        ArrayList<BigDecimal> scoreList = new ArrayList<>();
        scoreList.add(governanceAssessGlobal.getScoreSpec());
        scoreList.add(governanceAssessGlobal.getScoreStorage());
        scoreList.add(governanceAssessGlobal.getScoreCalc());
        scoreList.add(governanceAssessGlobal.getScoreQuality());
        scoreList.add(governanceAssessGlobal.getScoreSecurity());

        jsonObject.put("scoreList", scoreList);


        return jsonObject.toJSONString();
    }


    //考评问题列表
    @GetMapping("problemList/{type}/{pageNo}/{pageSize}")
    public String getLastProblemListByType(
            @PathVariable("type") String type,
            @PathVariable("pageNo") Integer pageNo,
            @PathVariable("pageSize")Integer pageSize){

        Integer from = (pageNo - 1) * pageSize;

        List<GovernanceAssessDetail> governanceAssessDetailList = governanceAssessDetailService.list(new QueryWrapper<GovernanceAssessDetail>()
                .eq("governance_type", type)
                .lt("assess_score", 10)
                .inSql("assess_date", "select max(assess_date) from governance_assess_detail")
                .last("limit " + from + "," + pageSize)
        );

        return JSON.toJSONString(governanceAssessDetailList);
    }

    //考评问题个数
    @GetMapping("problemNum")
    public String problemNum(){

        //查询出来的Map结构
//       一行数据是一个Map集合  k:列名  v：列对应的值
        List<Map<String, Object>> mapList = governanceAssessDetailService.listMaps(new QueryWrapper<GovernanceAssessDetail>()
                .select("governance_type", "count(*) as ct")
                .lt("assess_score", 10)
                .inSql("assess_date", "select max(assess_date) from governance_assess_detail")
                .groupBy("governance_type")
        );

        //将map中的数据重新封装 k:老map中governance_type对应的value  v：老map中 ct 对应的value
        HashMap<String, Object> resultMap = new HashMap<>();
        for (Map<String, Object> oldMap : mapList) {
            resultMap.put(oldMap.get("governance_type")+"", oldMap.get("ct"));
        }


        return JSON.toJSONString(resultMap);
    }

    //分组人员排行榜
    @GetMapping("rankList")
    public String rankList(){
        List<Map<String, Object>> mapList = governanceAssessTecOwnerService.listMaps(new QueryWrapper<GovernanceAssessTecOwner>()
                .select("if(tec_owner is null,'未分配技术负责人',tec_owner) as tecOwner", "score")
                .inSql("assess_date", "select max(assess_date) from governance_assess_tec_owner")
                .orderByDesc("score")
        );


        return JSON.toJSONString(mapList);
    }
}
